const fs = require('fs');
// 项目名称
var argvLen = process.argv.length;
var takeProjectName = "";
if (argvLen >= 3) {
	takeProjectName = process.argv[process.argv.length - 1];
}
let fileList = [{
		name: "manifest.json",
		path: "manifest.json"
	},
	{
		name: "baseUrl.js",
		path: "config/baseUrl.js"
	},
	{
		name: "mixin.scss",
		path: "style/mixin.scss"
	},
	{
		name: "pages.json",
		path: "pages.json"
	},
	{
		name: "debug",
		path: "unpackage/debug"
	},
	{
		name: "icons",
		path: "unpackage/res/icons"
	},
	{
		name: "theme",
		path: "static/icon/theme"
	},
	{
		name: "tab",
		path: "static/icon/tab"
	}
];
saveProject();
// 保存当前项目
function saveProject() {
	getCurrentName(function(projectName) {
		if (!fs.existsSync("manyProject/" + projectName)) {
			fs.mkdirSync("manyProject/" + projectName);
		}
		let len = fileList.length;
		recursive(0);
		// 递归
		function recursive(i) {
			let path = fileList[i].path;
			checkDirectory(path, "manyProject/" + projectName + "/" + fileList[i].name, function() {
				if (i < len - 1) {
					recursive(i + 1);
				} else {
					console.log(projectName + "=>储存完成");
					if (takeProjectName) {
						// 取出指定项目文件
						takeProject(takeProjectName);
					}
				}
			});
		}
	});
}
// 读取当前项目获取项目名称
function getCurrentName(callback) {
	if (fileList[0].name != "manifest.json") {
		console.log("数组第一个文件名必须是:manifest.json");
		return;
	}
	fs.readFile(fileList[0].path, function(err, data) {
		if (err) {
			console.log("文件读取失败:" + fileList[0].path);
			return false;
		}
		try {
			let content = data.toString();
			let contentStr = content.replace(/(\r|\n|\s)\/\/(.*?)(\r|\n)/g, "");
			let contentJson = JSON.parse(contentStr);
			if (contentJson.name == takeProjectName) {
				console.error('---------当前项目已是<' + takeProjectName + '>-----------');
			} else {
				callback(contentJson.name);
			}
		} catch (e) {
			console.log("获取项目名称失败", e);
		}
	});
}
// 读取文件
function takeProject(projectName, callback) {
	if (fs.existsSync("manyProject/" + projectName)) {
		let len = fileList.length;
		recursive(0);
		// 递归
		function recursive(i) {
			let path = fileList[i].path;
			delDir(path, function() {
				checkDirectory("manyProject/" + projectName + "/" + fileList[i].name, path, function() {
					if (i < len - 1) {
						recursive(i + 1);
					} else {
						console.log(projectName + "=>取出完成");
					}
				});
			});
		}
	} else {
		console.log("-------------未找到项目<" + projectName + ">----------");
	}
}
// 复制
function copy(src, dst, callback) {
	fs.stat(src, function(fileErr, fileStats) { //fileStats  该对象 包含文件属性
		if (fileErr) {
			callback && callback();
			return;
		};
		if (fileStats.isFile()) {
			console.log(src + "  -----写入-----    " + dst);
			let readable = fs.readFileSync(src); //创建读取流
			fs.writeFileSync(dst, readable); //创建写入流
			callback && callback();
		} else if (fileStats.isDirectory()) { //是目录则 递归
			let paths = fs.readdirSync(src); //同步读取当前目录
			let len = paths.length;
			if (len > 0) {
				recursive(0);
				// 递归
				function recursive(index) {
					var _src = src + '/' + paths[index];
					var _dst = dst + '/' + paths[index];
					fs.stat(_src, function(err, stats) { //stats  该对象 包含文件属性
						if (err) throw err;
						if (stats.isFile()) { //如果是个文件则拷贝 
							let readable = fs.readFileSync(_src); //创建读取流
							fs.writeFileSync(_dst, readable); //创建写入流
							console.log(_src + "  -----写入-----    " + _dst);
							if (index < len - 1) {
								recursive(index + 1);
							} else {
								callback && callback();
							}
						} else if (stats.isDirectory()) { //是目录则 递归
							checkDirectory(_src, _dst, function() {
								if (index < len - 1) {
									recursive(index + 1);
								} else {
									callback && callback();
								}
							});
						}
					});
				}
			} else {
				callback && callback();
			}
		}
	});
}
// 获取文件信息
function checkDirectory(src, dst, callback) {
	fs.access(dst, fs.constants.F_OK, (err) => {
		if (err) {
			let dstArr = dst.split("/");
			if (dstArr[dstArr.length - 1].indexOf(".") == -1) {
				fs.mkdirSync(dst);
			}
			copy(src, dst, callback);
		} else {
			copy(src, dst, callback);
		}
	});
};
// 同步删除文件下文件
function delDir(path, callback) {
	let files = [];
	if (fs.existsSync(path)) {
		fs.stat(path, function(fileErr, fileStats) { //fileStats  该对象 包含文件属性
			if (fileErr) throw fileErr;
			if (fileStats.isFile()) {
				callback && callback();
			} else if (fileStats.isDirectory()) {
				files = fs.readdirSync(path);
				let len = files.length;
				if (len > 0) {
					recursive(0);
					// 递归
					function recursive(index) {
						let curPath = path + "/" + files[index];
						if (fs.statSync(curPath).isDirectory()) {
							delDir(curPath); //递归删除文件夹
						} else {
							fs.unlinkSync(curPath); //删除文件
						}
						if (index < len - 1) {
							recursive(index + 1);
						} else {
							callback && callback();
						}
					}
				} else {
					callback && callback();
				}
			}
		})
	}
}
